package com.allwees.bs.c.module.ops.service.impl;

import com.allwees.bs.c.module.ops.entity.SubscribeEntity;
import com.allwees.bs.c.module.ops.repository.SubscribeRepository;
import com.allwees.bs.c.module.ops.service.ISubscribeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SubscribeServiceImpl implements ISubscribeService {

    @Autowired
    private SubscribeRepository subscribeRepository;

    @Override
    public void subscribe(String email, String userUuid) {
        if (StringUtils.isBlank(email)) {
            return;
        }
        SubscribeEntity subscribe = subscribeRepository.findKey(email, 1);
        if(subscribe != null){
            return;
        }
        subscribe = new SubscribeEntity();
        subscribe.setUserUuid(userUuid);
        subscribe.setSubKey(email);
        subscribeRepository.save(subscribe);
    }

    @Override
    public void cancelSubscribe(String email) {
        SubscribeEntity subscribe = subscribeRepository.findKey(email, 1);
        subscribe.setIsDeleted(1);
        subscribeRepository.save(subscribe);
    }
}
